﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace ParieurPro.DAL.Util
{
    class DatabaseHelper
    {
        /// <summary>
        /// Renvoie la clause IN d'une requete SQL à partir d'une liste d'objet.
        /// Tient compte de la limite à 1000 éléments.
        /// </summary>
        /// <param name="strOut"></param>
        /// <param name="field"></param>
        /// <param name="idList"></param>
        public static void GetInSQLFromObject(ref StringBuilder strOut, string field, List<int> idList)
        {
            int numberOfElements = 1;
            strOut.AppendFormat(CultureInfo.InvariantCulture, "({0} in (", field);
            for (int i = 0; i <= idList.Count - 1; i++)
            {
                if (i == 0)
                {
                    strOut.AppendFormat(CultureInfo.InvariantCulture, "{0}", idList[i]);
                }
                else if ((numberOfElements == 1000))
                {
                    strOut.AppendFormat(CultureInfo.InvariantCulture, ") OR {0} in ({1}", field, idList[i]);
                    numberOfElements = 1;
                }
                else
                {
                    strOut.AppendFormat(CultureInfo.InvariantCulture, ", {0}", idList[i]);
                    numberOfElements = numberOfElements + 1;
                }

            }
            strOut.Append("))");

        }

    }
}
